package com.joker.orange.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.joker.common.core.page.TableDataInfo;
import com.joker.common.core.domain.PageQuery;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.joker.common.utils.StringUtils;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import com.joker.orange.domain.bo.ShortVideoAdvertisingBo;
import com.joker.orange.domain.vo.ShortVideoAdvertisingVo;
import com.joker.orange.domain.ShortVideoAdvertising;
import com.joker.orange.mapper.ShortVideoAdvertisingMapper;
import com.joker.orange.service.IShortVideoAdvertisingService;

import java.util.List;
import java.util.Map;
import java.util.Collection;

/**
 * 短视频广告Service业务层处理
 *
 * @author Joker
 * @date 2024-06-09
 */
@RequiredArgsConstructor
@Service
public class ShortVideoAdvertisingServiceImpl implements IShortVideoAdvertisingService {

    private final ShortVideoAdvertisingMapper baseMapper;

    /**
     * 查询短视频广告
     */
    @Override
    public ShortVideoAdvertisingVo queryById(Long id){
        return baseMapper.selectVoById(id);
    }

    /**
     * 查询短视频广告列表
     */
    @Override
    public TableDataInfo<ShortVideoAdvertisingVo> queryPageList(ShortVideoAdvertisingBo bo, PageQuery pageQuery) {
        LambdaQueryWrapper<ShortVideoAdvertising> lqw = buildQueryWrapper(bo);
        Page<ShortVideoAdvertisingVo> result = baseMapper.selectVoPage(pageQuery.build(), lqw);
        return TableDataInfo.build(result);
    }

    /**
     * 查询短视频广告列表
     */
    @Override
    public List<ShortVideoAdvertisingVo> queryList(ShortVideoAdvertisingBo bo) {
        LambdaQueryWrapper<ShortVideoAdvertising> lqw = buildQueryWrapper(bo);
        return baseMapper.selectVoList(lqw);
    }

    private LambdaQueryWrapper<ShortVideoAdvertising> buildQueryWrapper(ShortVideoAdvertisingBo bo) {
        Map<String, Object> params = bo.getParams();
        LambdaQueryWrapper<ShortVideoAdvertising> lqw = Wrappers.lambdaQuery();
        lqw.like(StringUtils.isNotBlank(bo.getHyperlinkAddress()), ShortVideoAdvertising::getHyperlinkAddress, bo.getHyperlinkAddress());
        lqw.eq(StringUtils.isNotBlank(bo.getAdvertisingType()), ShortVideoAdvertising::getAdvertisingType, bo.getAdvertisingType());
        lqw.eq(StringUtils.isNotBlank(bo.getIsDisplay()), ShortVideoAdvertising::getIsDisplay, bo.getIsDisplay());
        lqw.orderByDesc(ShortVideoAdvertising::getCreateTime);
        return lqw;
    }

    /**
     * 新增短视频广告
     */
    @Override
    public Boolean insertByBo(ShortVideoAdvertisingBo bo) {
        ShortVideoAdvertising add = BeanUtil.toBean(bo, ShortVideoAdvertising.class);
        validEntityBeforeSave(add, bo.getImageUrlCopy());
        boolean flag = baseMapper.insert(add) > 0;
        if (flag) {
            bo.setId(add.getId());
        }
        return flag;
    }

    /**
     * 修改短视频广告
     */
    @Override
    public Boolean updateByBo(ShortVideoAdvertisingBo bo) {
        ShortVideoAdvertising update = BeanUtil.toBean(bo, ShortVideoAdvertising.class);
        validEntityBeforeSave(update, bo.getImageUrlCopy());
        return baseMapper.updateById(update) > 0;
    }

    /**
     * 保存前的数据校验
     */
    private void validEntityBeforeSave(ShortVideoAdvertising entity, String imageUrlCopy){
        entity.setImageUrl(imageUrlCopy);
    }

    /**
     * 批量删除短视频广告
     */
    @Override
    public Boolean deleteWithValidByIds(Collection<Long> ids, Boolean isValid) {
        if(isValid){
            //TODO 做一些业务上的校验,判断是否需要校验
        }
        return baseMapper.deleteBatchIds(ids) > 0;
    }
}
